package com.duke.shaking.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Handler;

import com.loopj.android.http.AsyncHttpResponseHandler;

public class StaticDocReader {
	private Handler handler; 
	public StaticDocReader() {
		
	}
	
	private class ReadResultRunnable implements Runnable {

		private AsyncHttpResponseHandler responseHandler;
		private boolean successful;
		private String result;
		private Throwable exp;
		
		public ReadResultRunnable(AsyncHttpResponseHandler responseHandler, boolean successful, String result, Throwable exp) {
			this.responseHandler = responseHandler;
			this.successful = successful;
			this.result = result;
			this.exp = exp;
		}
		public void run() {
			// TODO Auto-generated method stub
			if (successful) {
				responseHandler.onSuccess(200, result);
			}
			else {
				responseHandler.onFailure(exp, result);
			}
		}
		
	}
	/**
	 * MOck the post request to read the local dummy data
	 * @param context The android context
	 * @param url The local file relative url to assets
	 * @param contentType The content type, 
	 * @param responseHandler The response handler
	 */
	public void post(Context context, String url, String contentType, AsyncHttpResponseHandler responseHandler) {
		Resources resources = context.getResources();
		AssetManager assetManager = resources.getAssets();
		StringBuilder re = new StringBuilder();
		try {
		    InputStream inputStream = assetManager.open(url);
		    Reader reader = new InputStreamReader(inputStream);
		    int tempchar;
	        while ((tempchar = reader.read()) != -1) {
	            if (((char) tempchar) != '\r') {
	                re.append((char)tempchar);
	            }
	        }
	        reader.close();
	        if (handler == null) {
	        	handler = new Handler();
	        }
	        handler.post(new ReadResultRunnable(responseHandler, true, re.toString(), null));
	        
		} catch (IOException e) {
		    e.printStackTrace();
		    //responseHandler.onFailure(e, re.toString());
		    handler.post(new ReadResultRunnable(responseHandler, false, re.toString(), e));
		}
		
	}
	
	public String read(Context context, String url, String contentType) {
		Resources resources = context.getResources();
		AssetManager assetManager = resources.getAssets();
		StringBuilder re = new StringBuilder();
		try {
		    InputStream inputStream = assetManager.open(url);
		    Reader reader = new InputStreamReader(inputStream);
		    int tempchar;
	        while ((tempchar = reader.read()) != -1) {
	            if (((char) tempchar) != '\r') {
	                re.append((char)tempchar);
	            }
	        }
	        reader.close();
	        return re.toString();
		} catch (IOException e) {
		    e.printStackTrace();
		}
		return null;
		
	}
}
